package com.one.blocks.rbac.controller;

import com.one.blocks.common.request.PageRequest;
import com.one.blocks.common.response.ResponseHelper;
import com.one.blocks.common.response.WebPageResponse;
import com.one.blocks.common.response.WebResponse;
import com.one.blocks.mvc.controller.BaseController;
import com.one.blocks.mvc.valid.Custom;
import com.one.blocks.mvc.valid.Update;
import com.one.blocks.mvc.valid.UpdateSelective;
import com.one.blocks.mvc.valid.constraints.NoAllocatedData;
import com.one.blocks.rbac.controller.query.SysRoleQuery;
import com.one.blocks.rbac.controller.vo.SysRoleGrantVo;
import com.one.blocks.rbac.controller.vo.SysRoleVo;
import com.one.blocks.rbac.convert.SysRoleConvert;
import com.one.blocks.rbac.domain.SysRole;
import com.one.blocks.rbac.log.Log;
import com.one.blocks.rbac.manager.SysRoleManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2023-10-25 10:41:50
 */
@Api(tags = "系统角色接口")
@RestController
@RequestMapping("/sys/role")
@Slf4j
@Validated
@Log(module = "系统角色")
public class SysRoleController extends BaseController<SysRoleManager, SysRoleConvert, SysRole, SysRoleVo, SysRoleQuery> {

    @Autowired
    public SysRoleController(SysRoleManager manager) {
        super(manager, SysRoleConvert.INSTANCE);
    }

    @ApiOperation("查询单个系统角色")
    @PreAuthorize("sys:role:view")
    @GetMapping
    public WebResponse<SysRoleVo> get(@RequestParam Integer id) {
        return super.generalGet(id);
    }

    @ApiOperation("根据id批量查询系统角色")
    @PreAuthorize("sys:role:view")
    @GetMapping("/batch")
    public WebResponse<List<SysRoleVo>> getByIds(@Validated @NotNull @Size(min = 1, max = 100) @RequestParam("ids") Set<Integer> ids) {
        return super.generalGetIds(ids);
    }

    @ApiOperation("分页查询系统角色")
    @PreAuthorize("sys:role:view")
    @GetMapping("/page")
    public WebPageResponse<SysRoleVo> page(@Validated PageRequest request, @Validated SysRoleQuery query) {
        return super.generalPage(request, query);
    }

    @ApiOperation("添加系统角色")
    @PreAuthorize("sys:role:insert")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Serializable> insert(@Validated @RequestBody SysRoleVo sysRoleVo) {
        return super.generalInsert(sysRoleVo);
    }

    @ApiOperation("更新系统角色(更新所有字段)")
    @PreAuthorize("sys:role:update")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Void> update(@Validated(Update.class) @RequestBody SysRoleVo sysRoleVo) {
        return super.generalUpdate(sysRoleVo);
    }

    @ApiOperation("更新系统角色(只更新非空字段)")
    @PreAuthorize("sys:role:update")
    @PatchMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Void> updateSelective(@Validated(UpdateSelective.class) @RequestBody SysRoleVo sysRoleVo) {
        return super.generalUpdateSelective(sysRoleVo);
    }

    @ApiOperation("根据id批量删除系统角色")
    @PreAuthorize("sys:role:delete")
    @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Void> deleteByIds(
            @Validated @NoAllocatedData(manager = SysRoleManager.class)
            @NotNull @Size(min = 1, max = 100) @RequestBody Set<Integer> ids) {
        return super.generalDeleteByIds(ids);
    }

    @ApiOperation("指定角色授权用户")
    @PreAuthorize("sys:role:grant")
    @PostMapping("/grant")
    public WebResponse<Void> grant(@Validated({Default.class, Custom.class}) @RequestBody SysRoleGrantVo sysRoleGrantVo) {
        manager.grant(sysRoleGrantVo.getUserId(), convert.grantVoToGrantEntity(sysRoleGrantVo));
        return ResponseHelper.success();
    }
}